//
//  PredicateHelper.swift
//  HuaDaoPeifu
//
//  Created by 刘杰 on 2018/7/17.
//  Copyright © 2018年 paiyipai. All rights reserved.
//

import UIKit

class PredicateHelper: NSObject {
    
    /// 验证是否是有效的电话号码
    class func validateTelNumber(telnum:String)->Bool {
        let mobile = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$"
        let  CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$"
        let  CU = "^1(3[0-2]|5[256]|8[56])\\d{8}$"
        let  CT = "^1((33|53|8[09])[0-9]|349)\\d{7}$"
        let regextestmobile = NSPredicate(format: "SELF MATCHES %@",mobile)
        let regextestcm = NSPredicate(format: "SELF MATCHES %@",CM )
        let regextestcu = NSPredicate(format: "SELF MATCHES %@" ,CU)
        let regextestct = NSPredicate(format: "SELF MATCHES %@" ,CT)
        if ((regextestmobile.evaluate(with: telnum) == true)
            || (regextestcm.evaluate(with: telnum)  == true)
            || (regextestct.evaluate(with: telnum) == true)
            || (regextestcu.evaluate(with: telnum) == true))
        {
            return true
        }
        else
        {
            return false
        }
    }
    
    /// 验证是否是有效的电子邮箱
    class func validateEmail(email: String) -> Bool {
        let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        let emailTest:NSPredicate = NSPredicate(format: "SELF MATCHES %@", emailRegex)
        return emailTest.evaluate(with: email)
    }
    
    /// 验证身份证
    class func validateId(id:String) -> Bool {
        
        let idRegex = "(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)"
        let idTest:NSPredicate = NSPredicate(format: "SELF MATCHES %@", idRegex)
        return idTest.evaluate(with:id)
        
    }
    
    /// 验证银行卡
    class func validateBankCard(bankCard:String) -> Bool {
        
        let bankCardRegex = "^([0-9]{16}|[0-9]{19}|[0-9]{17}|[0-9]{18}|[0-9]{20}|[0-9]{21})$"
        let bankCardTest:NSPredicate = NSPredicate(format: "SELF MATCHES %@", bankCardRegex)
        return bankCardTest.evaluate(with:bankCard)
        
    }
    

    
}
